package com.dckj.auto.controller.help;

import com.alibaba.fastjson.JSONArray;
import com.dckj.auto.common.utils.SessionUtil;
import com.dckj.auto.common.utils.ehcache.EHCacheUtils;
import com.dckj.auto.service.AuthUserRoleService;
import com.xiaoleilu.hutool.setting.dialect.Props;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

@Component
public class UserMenuHelp {

    private static  Props props = new Props("config.properties","UTF-8");
    private static String auth_token_cache_key = props.getStr("auth_token_cache_key");

    @Autowired
    AuthUserRoleService userRoleService;
/**↓↓↓↓↓↓↓↓↓↓↓↓↓↓非controller层  @Autowired 注入 null 解决方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
    private static UserMenuHelp userMenuHelp;
    public UserMenuHelp(){}
    @PostConstruct
    public void init(){
        userMenuHelp = this;
        userMenuHelp.userRoleService = this.userRoleService;
    }
 /**↑↑↑↑↑↑↑↑↑↑↑↑↑↑非controller层  @Autowired 注入 null 解决方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/

    public static JSONArray userAuthMenu(HttpServletRequest request,Integer attribute) {
        String userId = SessionUtil.userId(request);
        Integer userType = SessionUtil.userType(request);
        EHCacheUtils cache = EHCacheUtils.getInstance();
        Object o = cache.get(auth_token_cache_key, userId);
        if(null == o){
            JSONArray jsonArray = userMenuHelp.userRoleService.selectUserAuto(userId, userType, attribute);
            if(null == jsonArray){
                return null;
            }
            cache.put(auth_token_cache_key,userId,jsonArray);
            return jsonArray;
        }
        return JSONArray.parseArray(o.toString());
    }
}
